/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yuhanginfo.modules.sys.web;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.modules.sys.entity.Area;
import com.yuhanginfo.modules.sys.entity.Store;
import com.yuhanginfo.modules.sys.entity.User;
import com.yuhanginfo.modules.sys.entity.YongUser;
import com.yuhanginfo.modules.sys.service.AreaService;
import com.yuhanginfo.modules.sys.service.StoreService;
import com.yuhanginfo.modules.sys.service.SystemService;
import com.yuhanginfo.modules.sys.service.YongUserService;
import com.yuhanginfo.modules.sys.utils.UserUtils;


/**
 * 后台用户管理
 * @author yangs
 * @version 2017-09-13
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/yongUser")
public class YongUserController extends BaseController {

	@Autowired
	private YongUserService userService;
	@Autowired
	private AreaService areaService;
	@Autowired
	private  StoreService storeService;
	@Autowired
	private SystemService systemService;
	
	private static final  String STATUS = "status";
	@ModelAttribute
	public YongUser get(@RequestParam(required=false) String id) {
		YongUser entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = userService.get(id);
		}
		if (entity == null){
			entity = new YongUser();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:yongUser:view")
	@RequestMapping(value = {"list", ""})
	public String list(YongUser user, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<Area> province=areaService.selectProvinceList();
		User us = UserUtils.getUser();
		List<Store> store=storeService.storeUserList(us.getId());
		user.setSysUserId(us.getId());
		Page<YongUser> page = userService.selectVip(new Page<YongUser>(request, response), user); 
		model.addAttribute("page", page);
		model.addAttribute("province", province);
		model.addAttribute("store", store);
		model.addAttribute("user", user);
		return "modules/sys/storeMessage/userList";
	}
    
	@RequiresPermissions("sys:yongUser:view")
	@RequestMapping(value ="cityData")
	@ResponseBody
	public List<Area> cityData(String id) {
		return areaService.selectCityList(id);
	}
	
	@RequiresPermissions("sys:yongUser:view")
	@RequestMapping(value ="personDate")
	public String personDate(Model model) {
		User us = UserUtils.getUser();
		User user=systemService.personDate(us.getId());
		if(user.getUserRole()==""||user.getUserRole()==null){
			user.setUserRole("无");
		}
		model.addAttribute("user", user);
		return "modules/sys/personData/personData";
	}
	
	@ResponseBody
	@RequiresPermissions("sys:yongUser:view")
	@RequestMapping(value ="pData")
	public Map<String,Object> pData(String yanMiMa,String password,String password1) {
		Map<String,Object> map=new HashMap<>();
		User us = UserUtils.getUser();
		
		if(!DigestUtils.md5Hex(yanMiMa).equals(us.getPassword())){
			map.put(STATUS, 1);
		}else{
		if(!password.equals(password1)){
			map.put(STATUS, 2);
		}else{
			if(DigestUtils.md5Hex(password).equals(us.getPassword())){
				map.put(STATUS, 3);
			}
		}
		}
		return map;
	}
	
	@RequiresPermissions("sys:yongUser:view")
	@RequestMapping(value ="updateView")
	public String updatePerson(Model model,User user) {
		model.addAttribute("user", user);
		return "modules/sys/personData/userModifyPwd";
	}
	
	@RequiresPermissions("sys:yongUser:view")
	@RequestMapping(value = "modifyPwd")
	public String modifyPwd(String newPassword, Model model) {
		User user = UserUtils.getUser();
		systemService.updatePasswordById(user.getId(), user.getLoginName(), newPassword);
		model.addAttribute("message", "修改密码成功");
		model.addAttribute("user", user);
		UserUtils.getSubject().logout();
		return "redirect:" + adminPath + "/login";
	}
	
	
	@ResponseBody
	@RequiresPermissions("sys:yongUser:view")
	@RequestMapping(value = "yzPassword")
	public Map<String,Object>yzPassword(String oldPassword){
		Map<String,Object>map=new HashMap<>();
		User user = UserUtils.getUser();
		String password=user.getPassword();
		boolean b=SystemService.validatePassword(oldPassword, password);
		if(b){
			map.put("stg", 0);
		}else{
			map.put("stg", 1);
		}
		return map;
	}
	
	@ResponseBody
	@RequiresPermissions("sys:yongUser:view")
	@RequestMapping(value = "storeList")
	public List<Store> storeList(String type){
		Store store=new Store();
		User us = UserUtils.getUser();
		store.setUserId(us.getId());
		if(type!=null&&type!=""){
			store.setType(type);
		}
		return storeService.selectByType(store);
	}
	
}